package com.hll.spring_book.util;

import com.hll.spring_book.pojo.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: Mr.Hu
 * @create: 2021-04-21 13:14
 */
public class SessionHelper {

  public static User getUser() {
    HttpServletRequest request = getRequest();
    return (User) request.getSession().getAttribute(Constants.USER);
  }

  public static String getToken() {
    HttpServletRequest request = getRequest();
    return (String) request.getSession().getAttribute(Constants.TOKEN);
  }

  private static HttpServletRequest getRequest() {
    return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  }
}
